package net.entelijan.cobean.bind.impl.remove;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.junit.Assert;

public class InvisibleSingleSelectionComponentBeanTestCase {

    @Test
    public void testSetValue() {
        StringBuffer sb = new StringBuffer();
        RecordingPropertyChangeListener l = new RecordingPropertyChangeListener(sb, "irrelevant");
        InvisibleSingleSelectionComponentBean b = new InvisibleSingleSelectionComponentBean();
        b.setPropertyChangeListener(l);
        b.setValue(createSimpleBeans("A", "B", "C"));
        Assert.assertEquals("1" + "null"
                + "[SimpleBean [strVal=A], SimpleBean [strVal=B], SimpleBean [strVal=C]]", sb
                .toString());
    }

    @Test
    public void testSetSelectedValue() {
        StringBuffer sb = new StringBuffer();
        RecordingPropertyChangeListener l = new RecordingPropertyChangeListener(sb, "irrelevant");
        InvisibleSingleSelectionComponentBean b = new InvisibleSingleSelectionComponentBean();
        b.setPropertyChangeListener(l);
        b.setSelectedValue(new SimpleBean("A"));
        Assert.assertEquals("1" + "null" + "SimpleBean [strVal=A]", sb.toString());
    }

    @Test
    public void testChangeListener() {
        StringBuffer sb = new StringBuffer();
        RecordingPropertyChangeListener l1 = new RecordingPropertyChangeListener(sb, "irrelevant");
        RecordingPropertyChangeListener l2 = new RecordingPropertyChangeListener(sb, "irrelevant");
        InvisibleSingleSelectionComponentBean b = new InvisibleSingleSelectionComponentBean();
        b.setPropertyChangeListener(l1);
        b.setSelectedValue(new SimpleBean("A"));
        Assert.assertEquals("1" //
                + "null" // 
                + "SimpleBean [strVal=A]"//
        , sb.toString());
        b.setPropertyChangeListener(l2);
        Assert.assertEquals("1" //
                + "null" // 
                + "SimpleBean [strVal=A]"//
                + "R"//
        , sb.toString());
        b.setSelectedValue(new SimpleBean("B"));
        Assert.assertEquals("1" //
                + "null" // 
                + "SimpleBean [strVal=A]"//
                + "R"//
                + "1" //
                + "SimpleBean [strVal=A]" // 
                + "SimpleBean [strVal=B]"//
        , sb.toString());
    }

    @Test
    public void testSetSelectedValueTwoTimes() {
        StringBuffer sb = new StringBuffer();
        RecordingPropertyChangeListener l = new RecordingPropertyChangeListener(sb, "irrelevant");
        InvisibleSingleSelectionComponentBean b = new InvisibleSingleSelectionComponentBean();
        b.setPropertyChangeListener(l);
        b.setSelectedValue(new SimpleBean("A"));
        b.setSelectedValue(new SimpleBean("B"));
        Assert.assertEquals("1" + //
                "null" + //
                "SimpleBean [strVal=A]" + //
                "1" + //
                "SimpleBean [strVal=A]" + //
                "SimpleBean [strVal=B]"//
        , sb.toString());
    }

    private List<SimpleBean> createSimpleBeans(String... strVals) {
        ArrayList<SimpleBean> re = new ArrayList<SimpleBean>();
        for (String strVal : strVals) {
            re.add(new SimpleBean(strVal));
        }
        return re;
    }

}
